/*
  (C) 2007 
  @author Michael Ocean

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package sxe.core.image;

import snobject.snNil;
import snobject.snObject;
import snobject.snObjectArgList;
import snobject.snSensor;
import step.EvaluationFailure;
import step.FunctionWrapper;
import sxe.sensors.GenericSensor;
import sxe.sensors.ImageSensor;
import sxe.sensors.SensorDetector;

public class get extends FunctionWrapper {	
	
	public snObject Call(snObjectArgList arg) throws EvaluationFailure {
		snSensor sens = arg.popSensor();
		
		String sSensorID = sens.getSensorID();
		String sSensorType = sens.getSensorType();
		String sFormat = null;
		
		GenericSensor gs = SensorDetector.GetDevice(sSensorID, sSensorType);
		if(gs instanceof ImageSensor){
			sFormat =((ImageSensor)gs).getResolution();
		}
		
		if(gs==null){
			System.err.println("Could not locate a sensor with ID '" + sSensorID + "' of type " + sSensorType );
			throw new EvaluationFailure(this.expnode,"Couldn't get sensor " + sSensorID);
		}
		
		if(! (gs.getType().equals(GenericSensor.TYPE_IMAGE)
			|| gs.getType().equals(GenericSensor.TYPE_IMAGE_PTZ))) 
		{
			System.err.println("Sensor " + sSensorID + "is not of type Image");
			throw new EvaluationFailure(this.expnode,"Sensor " + sSensorID + "is not of type Image");
		}
		/* grab a frame from the framegrabber */
		snObject snRet = null;
		
		snRet = gs.getData(sFormat);
		
		if(snRet.equals(snNil.NIL))
			throw new EvaluationFailure(this.expnode,"grabber failed to convert image to buffered image");
		return snRet;
	}
	
}
